<?php

namespace App\Repositories\Admin;

use App\BaseRepository;
use App\Helpers\Helpers;
use App\Model\ProductAttrLabel;
use App\Model\ProductAttrValue;
use App\Model\ProductCategoryRelation;
use App\Model\ProductSku;
use App\Model\ProductSkuAttr;
use App\Model\ResourceTable;
use App\Model\SysRegion;
use App\Model\WineProduct;
use App\Model\WineProductChateauRelation;
use App\Model\WineProductCuisineRelation;
use App\Model\WineProductDealerRelation;
use App\Model\WineProductDeployment;
use App\Model\WineProductImporterRelation;
use App\Model\WineProductRelation;
use App\Model\WineProductSceneRelation;
use App\Model\WineProductUserTag;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;

class WineProductRepository extends BaseRepository
{
    //模型名称
    protected $modelName = "App\Model\WineProduct";

    protected $appendCreatorAndUpdater = true;

    protected $appendUserId = true;

    protected function beforeListShow($model)
    {
        return $model->orderBy('weight','desc')->orderBy('created_at','desc');
    }

    protected function commonRules()
    {
        return [
            'chname' => 'required',
            'banner' => 'required',
            'category_id' => 'required',
            'attr_group_id' =>[
                'required',
                Rule::exists('product_attr_groups','id')->whereNull('deleted_at'),
            ]
        ];
    }

    protected function appendStoreFillData()
    {
        $attr['state'] = 'draft';
        if ($this->loginUser->userType == "admin"){
            $attr['state'] = "publish";
        }
        if ($this->loginUser->userType == "editor"){
            $attr['state'] = "pending";
        }
        $attr['productCode'] = Helpers::createSerial('wine_product','productCode','P'.date('Y'));
        if (!$this->request->input('online')){
            $attr['online'] = "no";
        }

        return $attr;
    }

    protected function commonMessages()
    {
        return [
            'chname.required' => '产品名称必填',
        ];
    }

    protected function commonSave($model)
    {
        if (!$model->productCode){
            $model->productCode = Helpers::createSerial('wine_product','productCode','P'.date('Y'));
            $model->save();
        }

        $attr = [1=>'车载',2=>'家用'];
        if($model->category_id){
            $model->category_name = isset($attr[$model->category_id])?$attr[$model->category_id]:'';
            $model->save();
        }

        $model->name = $model->chname;
        $model->save();

        WineProduct::updateSkuCount($model->id);
        WineProduct::updateOrderCount($model->id);

    }

    protected function destroyValidate($id)
    {
        $c = DB::table('wine_focus_picture')->where('obj_id','=',$id)->where('type','=','product')
            ->whereNull('deleted_at')->count();

        if($c>0) {
            $this->error = "此产品已关联焦点图";
            return false;
        }

        $c = DB::table('orders')->where('product_id','=',$id)->whereNull('deleted_at')->count();

        if($c>0) {
            $this->error = "此产品已有相关订单，不可删除";
            return false;
        }

        return true;
    }

}